package com.xy.hms.controller;


import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xy.hms.model.Staff;
import com.xy.hms.service.StaffServiceI;
import com.xy.hms.util.RedisCache;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 员工表(Staff)表控制层
 *
 * @author makejava
 * @since 2020-03-16 14:42:02
 */
@Api(tags = "员工管理")
@Slf4j
@Controller
public class StaffController {
    /**
     * 服务对象
     */
    @Autowired
    public StaffServiceI staffService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/staff/selectOne")
    public Staff selectOne(Integer id) {
        return this.staffService.queryById(id);
    }


    /**
     * 查出所有的员工，去所有员工页面
     */
    @GetMapping("/staff/staffs")
    public String toAllStaffs(Model model){

        List<Staff> allstaffs = staffService.queryAll(new Staff());
        model.addAttribute("allstaffs",allstaffs);
        return "staff/all_staffs";
    }

    @PostMapping("/staff/addStaff")
    public String addStaff(Staff staff,Model model){
//        staffService.insert(staff);
        log.info(staff.toString());
        try {
            String message = staffService.insert(staff);
            //新增成功，到显示所有的界面
            return "redirect:/staff/staffs";

        } catch (Exception e) {
            model.addAttribute("failaddStaff",e.getMessage());
            //添加失败，显示失败信息
            return "staff/add_staff";
        }

    }


    /**
     * 跳转到跳转到修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/staff/{id}")
    public String toEditPage(@PathVariable("id")Integer id,Model model){
        //查询需要修改的员工
        Staff staff = staffService.queryById(id);
        model.addAttribute("toUpdateStaff",staff);
//        System.out.println(staff);
        //返回到修改页面
        return "staff/edit_staff";
    }


    /**
     * 修改员工
     * @param staff
     * @param model
     * @return
     */
    @PutMapping("/staff/updateStaff")
    public String updateStaff(Staff staff,Model model){

        String update = staffService.update(staff);
        model.addAttribute("updatemsg",update);

        return "redirect:/staff/staffs";
    }


    @GetMapping("/staff/deleteStaff/{id}")
    public String deleteStaff(@PathVariable("id")Integer id){

        boolean b = staffService.deleteById(id);
        return "redirect:/staff/staffs";
    }




}
